package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.ReturnCode;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.entity.MisMannageEntity.User;
import com.javasm.service.MisMannageService.EditPwdService;
import com.javasm.service.MisMannageService.impl.EditPwdServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author:JAVASM
 * @description:
 * @date:16:26
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/editpwd")
public class EditPwdServlet extends BaseServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        ReturnEntity re=new ReturnEntity();
        String uname = req.getParameter("userName");
        String upwd = req.getParameter("userPwd");

        User insertUser = new User(uname, upwd);
        EditPwdService edit=new EditPwdServiceImpl();

        insertUser.setUserPwd(upwd);
        Integer res=edit.Edit(insertUser);
        if (res>0){
            re.setRetCode(ReturnCode.EDIT_OK.getCode());
            re.setRetMsg(ReturnCode.EDIT_OK.getMsg());
        }else {
            re.setRetCode(ReturnCode.EDIT_FAILED.getCode());
            re.setRetMsg(ReturnCode.EDIT_FAILED.getMsg());
        }
        String resstr = JSON.toJSONString(re);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer=resp.getWriter();
        writer.print(resstr);
        writer.flush();
        writer.close();
    }
}
